package com.yf1706.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yf1706.bean.CountIncomeTaxRate;
import com.yf1706.service.TaxRatesService;
import com.yf1706.tools.JsonTool;

/**
 * 
 * @author 锦涛
 *
 */
@Controller
@RequestMapping("taxRate")
public class TaxRateController extends BaseController{
	
	@Autowired
	private TaxRatesService taxRateService;
	
	@RequestMapping("querybypage")
	@ResponseBody
	public Map<String, Object> querybypage(int page,int rows,String sort,String order) {
		Page<CountIncomeTaxRate> pages = taxRateService.querybypage(page, rows, sort, order);
		Long total= pages.getTotalElements();
		List<CountIncomeTaxRate> list = pages.getContent();
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}

	@RequestMapping("insert")
	@ResponseBody
	public void insert(HttpServletRequest request,HttpServletResponse response) {
		JSONObject jObject = JSON.parseObject(request.getParameter("djt_tax_rowData"));
		CountIncomeTaxRate taxRate = new CountIncomeTaxRate();
		taxRate.setStepMin(jObject.getIntValue("stepMin"));
		taxRate.setStepMax(jObject.getIntValue("stepMax"));
		taxRate.setCutoffPoint(jObject.getIntValue("cutoffPoint"));
		taxRate.setQuickDeduction(jObject.getIntValue("quickDeduction"));
		taxRate.setTaxRates(jObject.getDoubleValue("taxRates"));
		JsonTool jTool = new JsonTool();
		try {
			//taxRateService.save(taxRate);
			jTool.setSuccess(true);
			jTool.setMsg("添加税率为【"+taxRate.getTaxRates()+"】成功！");
		} catch (Exception e) {
			jTool.setMsg("添加税率为【"+taxRate.getTaxRates()+"】失败！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}

	@RequestMapping("update")
	@ResponseBody
	public void update(HttpServletRequest request,HttpServletResponse response) {
		JSONObject jObject = JSON.parseObject(request.getParameter("djt_tax_rowData"));
		CountIncomeTaxRate taxRate = new CountIncomeTaxRate();
		taxRate.setStep(jObject.getIntValue("step"));
		taxRate.setStepMin(jObject.getIntValue("stepMin"));
		taxRate.setStepMax(jObject.getIntValue("stepMax"));
		taxRate.setCutoffPoint(jObject.getIntValue("cutoffPoint"));
		taxRate.setQuickDeduction(jObject.getIntValue("quickDeduction"));
		taxRate.setTaxRates(jObject.getDoubleValue("taxRates"));
		JsonTool jTool = new JsonTool();
		try {
			//taxRateService.update(taxRate);
			jTool.setSuccess(true);
			jTool.setMsg("编辑税率为【"+taxRate.getTaxRates()+"】成功！");
		} catch (Exception e) {
			jTool.setMsg("编辑税率为【"+taxRate.getTaxRates()+"】失败！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}

	@RequestMapping("delete")
	@ResponseBody
	public void delete(HttpServletRequest request,HttpServletResponse response) {
		String steps = removeFirstAndLastChar(request.getParameter("djt_tax_rowData"));
		JsonTool jTool = new JsonTool();
		try {
			//taxRateService.remove(steps);
			jTool.setSuccess(true);
			jTool.setMsg("删除级数为【"+steps+"】成功！");
		} catch (Exception e) {
			jTool.setMsg("删除级数为【"+steps+"】失败！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}
}
